package logging

import (
	"os"
	"sync"

	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

var (
	levels    = make(map[string]*zap.AtomicLevel)
	mutex     sync.Mutex
	stdWriter = zapcore.Lock(os.Stdout)
)

func RegisterScope(key string) *zap.SugaredLogger {
	mutex.Lock()
	defer mutex.Unlock()
	var level *zap.AtomicLevel
	var ok bool
	if level, ok = levels[key]; !ok {
		levelE := zap.NewAtomicLevel()
		level = &levelE
	}
	encoderCfg := zap.NewDevelopmentEncoderConfig()
	logger := zap.New(zapcore.NewCore(
		zapcore.NewConsoleEncoder(encoderCfg),
		stdWriter,
		level,
	), zap.AddCaller())
	return logger.Sugar()
}
